<template>
  <div>
    <van-nav-bar title="Operating File" left-arrow fixed @click-left="onClickLeft">
    </van-nav-bar>

    <div class="margin">
      <van-button plain hairline type="primary" @click="createAndWriteFile">创建文件</van-button>
    <van-button plain hairline type="primary" @click="getAndReadFile">读取文件</van-button>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return {
      tasksStr:'Hello World!'
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:'/'});
    }, 
    /*
    * 打开或创建文件夹,创建文件并写入内容
    * Android:sdcard/xbrother/assets目录
    * IOS:cdvfile://localhost/persistent/xbrother/assets目录
    * 文件目录存在则打开,不存在则创建
    * */
    createAndWriteFile() {
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (fs)=> {
        alert('打开的文件系统: ' + fs.name);
          fs.root.getDirectory('xbrother', {create: true}, (dirEntry)=> {
              dirEntry.getDirectory('assets', {create: true},  (subDirEntry)=> {
                  subDirEntry.getFile("task.json", {create: true, exclusive: false}, (fileEntry)=> {
                      fileEntry.name == 'task.json';
                      fileEntry.fullPath == 'xbrother/assets/task.json';
                      //文件内容
                      let dataObj = new Blob(['Hello World!'], {type: 'text/plain'});
                      //写入文件
                      this.writeFile(fileEntry, dataObj);
                  }, (err)=>{
                    alert("文件创建失败！");
                  });
              }, (err)=>{
                alert("文件夹创建失败！");
              });
          }, (err)=>{
                alert("文件夹创建失败！");
              });
      }, (err)=>{
        alert("文件系统加载失败！");
      });
    },
 
    /*
    * 依次打开指定目录文件夹,读取文件内容
    * Android:sdcard/xbrother/assets/task.json
    * IOS:cdvfile://localhost/persistent/xbrother/assets/task.json
    * 目录和文件存在则打开,不存在则退出
    * */
    getAndReadFile() {
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (fs)=> {
          alert('打开的文件系统: ' + fs.name);
          fs.root.getDirectory('xbrother', {create: false}, (dirEntry)=> {
              dirEntry.getDirectory('assets', {create: false}, (subDirEntry)=> {
                  subDirEntry.getFile("task.json", {create: false, exclusive: false},(fileEntry)=> {
                      alert("是否是个文件？"+ fileEntry.isFile.toString());
                      fileEntry.name == 'task.json';
                      fileEntry.fullPath == 'xbrother/assets/task.json';
                      this.readFile(fileEntry);
                  }, (err)=>{
                    alert("文件创建失败！");
                  });
              }, (err)=>{
                alert("文件夹创建失败！");
              });
          }, (err)=>{
                alert("文件夹创建失败！");
              });
      }, (err)=>{
        alert("文件系统加载失败！");
      });
    },
 
    //将内容数据写入到文件中
    writeFile(fileEntry, dataObj) {
      //创建一个写入对象
      fileEntry.createWriter(function (fileWriter) {
  
          //文件写入成功
          fileWriter.onwriteend = function () {
              alert("Successful file write...");
          };
  
          //文件写入失败
          fileWriter.onerror = function (e) {
              alert("Failed file write: " + e.toString());
          };
  
          //写入文件
          fileWriter.write(dataObj);
      });
    },
 
    //读取文件
    readFile(fileEntry) {
      fileEntry.file((file)=>{
          let reader = new FileReader();
          reader.onloadend = function () {
            alert("file read success:" + this.result);
          };
          reader.readAsText(file);
      }, (err)=>{
        alert("文件读取失败!");
      });
    }
  }
}
</script>

<style scoped lang="less">
.margin{
  margin-top: 60px;
}
</style>